<?php

namespace app\api\controller;


use app\api\logic\Wxpay;

class Index extends Base
{
    //支付宝支付异步回调
    public function alipay()
    {
        import('alipay/AopSdk', EXTEND_PATH);
        $aop = new \AopClient;
        $aop->alipayrsaPublicKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA5fpmP2im9x4I27+zF9AGZH3EZVQ1TN+KhRp3Kku2ZpoPbVXkHNitywMaTgIQk3D94hATenRAvb3YzioEj3mC1IrxzZXj35TXn89wzvzSsbbNZeFw1eMO/WtamhJYjHKjZsSeDvX7hIlQO5Dcoa3/IhWnChuGr2l0o2ROh6KU1nxYCTxnNdVC0/w7QiqPpSbluGZ/tW4mzYnS7rqX07aBMQXcvax8hgHllgetPLii8qGHDct1TEafpdSBHToisM6616i1aHy6QHFfK1sa99rT1mmDJJLttBileJEKiNHe1v2oom9V1cwau/cj/1wW7FwEhbu0zWoRtiy8gDhAJPxtIQIDAQAB';//支付宝公钥
        $flag = $aop->rsaCheckV1($_POST, NULL, "RSA2");
        $data = [
            'content' => serialize($_POST),
            'create_time' => date('Y-m-d H:i:s', time()),
        ];
        M('pay_callback_log')->insert($data);
        if ($flag) {
            $out_trade_no = $_POST['out_trade_no'];
            $array = explode('_', $out_trade_no);
            if ($array[1] == 1) {
                $status = M('goods_order')->where('order_number', $array[0])->value('status');
                if ($status > 0) {
                    exit('success');
                }
                $res = M('goods_order')->where('order_number', $array[0])->update(['status' => 1]);
                if (!empty($res)) {
                    exit('success');
                }
            } elseif ($array[1] == 2) {
                $status = M('course_order')->where('order_number', $array[0])->value('pay_status');
                if ($status == 1) {
                    exit('success');
                }
                $res = M('course_order')->where('order_number', $array[0])->update(['pay_status' => 1]);
                if (!empty($res)) {
                    exit('success');
                }
            }
        }
    }

    //微信支付异步回调
    public function wxpay()
    {
        $postXml = file_get_contents("php://input");    // 接受通知参数；
        $postObj = Wxpay::xmlToObject($postXml);      // 调用解析方法，将xml数据解析成对象
        $data = [
            'type' => 2,
            'content' => serialize($this->object_array($postObj)),
            'create_time' => date('Y-m-d H:i:s', time()),
        ];
        M('pay_callback_log')->insert($data);
        //确认支付成功
        if ($postObj->return_code == "SUCCESS" && $postObj->result_code == "SUCCESS") {

            $out_trade_no = $postObj->out_trade_no;
            $array = explode('_', $out_trade_no);
            if ($array[1] == 1) {
                $status = M('goods_order')->where('order_number', $array[0])->value('status');
                if ($status > 0) {
                    exit('<xml>
                      <return_code><![CDATA[SUCCESS]]></return_code>
                      <return_msg><![CDATA[OK]]></return_msg>
                    </xml>');
                }
                $res = M('goods_order')->where('order_number', $array[0])->update(['status' => 1]);
                if (!empty($res)) {
                    exit('<xml>
                      <return_code><![CDATA[SUCCESS]]></return_code>
                      <return_msg><![CDATA[OK]]></return_msg>
                    </xml>');
                }
            } elseif ($array[1] == 2) {
                $status = M('course_order')->where('order_number', $array[0])->value('pay_status');
                if ($status == 1) {
                    exit('<xml>
                      <return_code><![CDATA[SUCCESS]]></return_code>
                      <return_msg><![CDATA[OK]]></return_msg>
                    </xml>');
                }
                $res = M('course_order')->where('order_number', $array[0])->update(['pay_status' => 1]);
                if (!empty($res)) {
                    exit('<xml>
                      <return_code><![CDATA[SUCCESS]]></return_code>
                      <return_msg><![CDATA[OK]]></return_msg>
                    </xml>');
                }
            }

        }
    }
    public function redis(){
        $redis = new \Redis();
        $redis->connect('127.0.0.1',6379);
        for($i=0;$i<50;$i++) {
            try {
                $redis->LPUSH('msg', rand(1000, 5000));
            } catch (Exception $e) {
                echo $e->getMessage();
            }
        }
        $redis1 = new \Redis();
        $redis1->pconnect('127.0.0.1',6379);
        while(true) {
            try {
                $value = $redis1->LPOP('msg');
                if (!$value) {
                    break;
                }
                var_dump($value) . "\n";
                /*
                * 利用$value进行逻辑和数据处理
                */
            } catch (\Exception $e) {
                echo $e->getMessage();
            }
        }
    }
}
